package com.study.demo.aop.demo2;

import com.study.demo.aop.demo2.operationsource.ElvinOperationSource;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * @author: elvin
 * @time: 2019-05-23 14:28
 * @desc:
 **/
abstract class ElvinOperationSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
    @Override
    public boolean matches(Method method, @Nullable Class<?> targetClass) {
        ElvinOperationSource cas = getElvinOperationSource();
        return (cas != null && !CollectionUtils.isEmpty(cas.getElvinOperation(method, targetClass)));
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof ElvinOperationSourcePointcut)) {
            return false;
        }
        ElvinOperationSourcePointcut otherPc = (ElvinOperationSourcePointcut) other;
        return ObjectUtils.nullSafeEquals(getElvinOperationSource(), otherPc.getElvinOperationSource());
    }

    @Override
    public int hashCode() {
        return ElvinOperationSourcePointcut.class.hashCode();
    }

    @Override
    public String toString() {
        return getClass().getName() + ": " + getElvinOperationSource();
    }


    /**
     * Obtain the underlying {@link ElvinOperationSource} (may be {@code null}).
     * To be implemented by subclasses.
     */
    @Nullable
    protected abstract ElvinOperationSource getElvinOperationSource();
}
